#include "logindialog.h"


LoginDialog::LoginDialog(QWidget *parent) : QDialog(parent)
{
    setFixedSize(400, 300);
    setWindowTitle("登陆");
    welcomeLabel = new QLabel("欢迎使用汽车销售管理系统");
    welcomeLabel->setFixedHeight(40);
    welcomeLabel->setFrameStyle(QLabel::StyledPanel);
    welcomeLabel->setAlignment(Qt::AlignCenter);
    usernameLabel = new QLabel("用户名：");
    usernameLineEdit = new QLineEdit;
    passwordLabel = new QLabel("密码");
    passwordLineEdit = new QLineEdit;
    passwordLineEdit->setEchoMode(QLineEdit::Password);    //小黑点
    usernameLineEdit->setText("Administrator");

    okButton = new QPushButton("登陆");
    cancelButton = new QPushButton("取消");
    QGridLayout* mainLayout = new QGridLayout(this);
    mainLayout->addWidget(welcomeLabel, 1, 0, 1, 7);
    mainLayout->addWidget(usernameLabel, 2, 1, 1, 1);
    mainLayout->addWidget(usernameLineEdit, 2, 2, 1, 3);
    mainLayout->addWidget(passwordLabel, 3, 1, 1, 1);
    mainLayout->addWidget(passwordLineEdit, 3, 2, 1, 3);
    mainLayout->addWidget(okButton, 4, 2, 1, 1);
    mainLayout->addWidget(cancelButton, 4, 4, 1, 1);
    connect(okButton, SIGNAL(clicked(bool)), this, SLOT(slotOkButton()));
    connect(cancelButton, SIGNAL(clicked(bool)), this, SLOT(slotcancelButton()));
}

void LoginDialog::slotcancelButton()
{
    QDialog::reject();
}

void LoginDialog::slotOkButton()
{
    if(usernameLineEdit->text().isEmpty())
    {
        QMessageBox::information(this, "提示", "请输入用户名！", QMessageBox::Ok);
    }
    else
    {
        if(passwordLineEdit->text().isEmpty())
        {
            QMessageBox::information(this, "提示", "请输入密码！", QMessageBox::Ok);
        }
        else
        {
            QString username = usernameLineEdit->text();
            QString password = passwordLineEdit->text();
            QSqlQuery query;
            query.exec(QString("select password from passwordtable where username='%1'").arg(username));
            query.next();
            if(query.value(0).toString() == password)
            {
                QDialog::accept();
            }
            else
            {
                QMessageBox::warning(this, "密码错误！", "请输入正确密码！", QMessageBox::Ok);
                passwordLineEdit->clear();
                passwordLineEdit->setFocus();
            }
        }
    }
}
